<%@page import="multiface.valentinorusso.jpa.File"%>
<%@page import="multiface.valentinorusso.jpa.Lavoro"%>
<%@page import="java.util.List"%>
<%@page import="multiface.valentinorusso.jpa.Categoria"%>
<%@page import="javax.persistence.EntityManager"%>
<%@page import="multiface.valentinorusso.jpa.util.PersistenceManager"%>
<%@page import="javax.persistence.EntityManagerFactory"%>
<%
    EntityManagerFactory emf = PersistenceManager.getInstance().getEntityManagerFactory();
    EntityManager em = emf.createEntityManager();

    String id = request.getParameter("id");

    Categoria cat = em.find(Categoria.class, Integer.parseInt(id));
    em.refresh(cat);
    String desc= "";
    if(cat.getDescrizione()!=null){
        desc=cat.getDescrizione();
    }
    
    List<Lavoro> list = em.createNativeQuery("SELECT * FROM lavoro WHERE categoria=?", Lavoro.class).setParameter(1, Integer.parseInt(id)).getResultList();
%>
<style>
    .box-image{
        margin-top: 6px;
        margin-right: 6px;
        margin-bottom: 6px;
        height: 120;
        width: 150;
        float: left;
    }
    .desc-cat{
        padding: 6px;
    }
</style>


<div id="center" style="width: 620px;">

    <div class="desc-cat"><%=desc%></div>
    <%
        for (Lavoro l : list) {
            em.refresh(l);
    %>
    <div class="box-image">

        <%
            String src = "";
            List<File> lf = l.getFileList();
            if (lf.size() > 0) {
                src = "GetImageServlet?th=y&fid=" + lf.get(0).getIdfile();
                for (File f : lf) {
                    if (f.getCopertina()) {
                        src = "GetImageServlet?th=y&fid=" + f.getIdfile();
                    }
                }
            }
        %>
        <a href="?page=lavoro&id=<%=l.getIdlavoro()%>"><img  src="<%=src%>" width="120" height="90" /></a>
        <div class="box-image-title"><%=l.getNome()%></div>
    </div>
    <%
        }
    %>
</div>